# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html


# useful for handling different item types with a single interface
from itemadapter import ItemAdapter
from myScrapy.models.csdn_model import CsdnModel
from myScrapy.models.douban_comments_model import DoubanCommentsModel

import codecs
import json
import os


class MyscrapyPipeline:
    def process_item(self, item, spider):
        return item


class TestPipeline:
    def __init__(self):
        pass

    def process_item(self, item, spider):
        csnd_model = CsdnModel()
        csnd_model.name = item['name']
        csnd_model.index = item['index']
        csnd_model.num = item['num']
        csnd_model.hot = item['hot']

        csnd_model.add()
        return item

    def close_spider(self, spider):
        pass


class DoubanCommentsPipeline:
    # def __init__(self):
    #     pass

    # def process_item(self, item, spider):
    #     douban_comments_model = DoubanCommentsModel()

    #     douban_comments_model.comment = item['comment']
    #     douban_comments_model.comment_id = item['comment_id']
    #     douban_comments_model.people_name = item['people_name']
    #     douban_comments_model.people_url = item['people_url']
    #     douban_comments_model.score = item['score']
    #     douban_comments_model.comment_time = item['comment_time']
    #     douban_comments_model.votes = item['votes']
    #     douban_comments_model.object_id = item['object_id']
    #     douban_comments_model.add()
    #     return item

    # def close_spider(self, spider):
    #     pass
    # 构造方法，在调用类的时候只执行一次
    def __init__(self):
        super().__init__()  # 执行父类的构造方法
        self.fp = codecs.open('douban.json', 'w', encoding='utf-8')
        self.fp.write('[')
        # 来一个item就会调用一次这个方法

    def process_item(self, item, spider):
        # 将item转为字典
        d = dict(item)
        # 将字典转为json格式
        string = json.dumps(d, ensure_ascii=False)
        self.fp.write(string + ',\n')  # 每行数据之后加入逗号和换行
        return item

    def close_spider(self, spider):
        # self.fp.seek(-2, os.SEEK_END)  # 定位到倒数第二个字符，即最后一个逗号
        # self.fp.truncate()  # 删除最后一个逗号
        self.fp.write(']')  # 文件末尾加入一个‘]’
        self.fp.close()   # 关闭文件

class GamerskyCommentsPipeline:
    # 构造方法，在调用类的时候只执行一次
    def __init__(self):
        super().__init__()  # 执行父类的构造方法
        self.fp = codecs.open('gamersky_comments.json', 'w', encoding='utf-8')
        self.fp.write('[')
        # 来一个item就会调用一次这个方法

    def process_item(self, item, spider):
        # 将item转为字典
        d = dict(item)
        # 将字典转为json格式
        string = json.dumps(d, ensure_ascii=False)
        self.fp.write(string + ',\n')  # 每行数据之后加入逗号和换行
        return item

    def close_spider(self, spider):
        # self.fp.seek(-2, os.SEEK_END)  # 定位到倒数第二个字符，即最后一个逗号
        # self.fp.truncate()  # 删除最后一个逗号
        self.fp.write(']')  # 文件末尾加入一个‘]’
        self.fp.close()   # 关闭文件
